package com.caiheng.api.service.product.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.caiheng.api.dao.product.ProductTypeMapper;
import com.caiheng.api.entity.product.ProductType;
import com.caiheng.api.entity.dto.ProductTypeDto;
import com.caiheng.api.service.base.impl.BaseServiceImpl;
import com.caiheng.api.service.product.ProductTypeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class ProductTypeServiceImpl extends BaseServiceImpl<ProductTypeMapper, ProductType> implements ProductTypeService {

    @Resource
    private ProductTypeMapper productTypeMapper;

    @Override
    public List<ProductTypeDto> getChildOptions(int cateId,int cateChildId) {
        List<ProductTypeDto> list = new ArrayList<>();

        ProductTypeDto dto = new ProductTypeDto();
        dto.setText("全部");
        dto.setValue("0");
        list.add(dto);

        QueryWrapper<ProductType> qw = new QueryWrapper<>();
        qw.eq("state",0);
        qw.eq("parent_id",cateId);
        qw.orderByAsc("sort");
        List<ProductType> types = productTypeMapper.selectList(qw);
        if(CollUtil.isNotEmpty(types)){
            for (ProductType type : types){
                ProductTypeDto typeDto = new ProductTypeDto();
                typeDto.setText(type.getTypeName());
                typeDto.setValue(type.getId().toString());
                list.add(typeDto);
            }
        }
        return list;
    }
}
